package com.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dao.FieldsDao;
import com.dao.IssueDao;
import com.dao.StatusDao;
import com.entity.FieldsEntity;
import com.entity.IssueEntity;
import com.entity.StatusEntity;
import com.service.status.StatusCode;

/*
 * List<TicketDto> getOpen() done
 * List<TicketDto> getClosed() done
 * List<TicketDto> getResolved() done
 * List<UserDto> getWatchers()  no need of this
 * List<AttachmentDto> getAttachment() no need of this
 * List<CommentDto> getComments() no need of this
 *  List<TicketDto> getByPriority() 
 *  String getResolutionTime()
 */

@Service
public class TicketService {

	@Autowired
	private StatusDao statusDao;
	@Autowired
	private FieldsDao fieldsDao;
	@Autowired
	private IssueDao issueDao;

	/**
	 * returns Issue by it's Id
	 * 
	 * @param String
	 *            id
	 * 
	 * @return IssueEntity
	 */

	@Transactional
	public IssueEntity getIssueById(String id) {
		IssueEntity issue = issueDao.getEagerIssueByUniqueId(id);
		if (issue == null) {
			return null;
		}
		return issue;
	}

	@Transactional
	public IssueEntity getIssueByKey(String key) {
		IssueEntity issue = issueDao.getEagerIssueByUniqueKey(key);
		if (issue == null) {
			return null;
		}
		System.out.println("test print" + issue);
		return issue;
	}

	/**
	 * returns List of tickets for current status
	 * 
	 * @param status
	 *            for ex. Open, Closed, In Progress, Resolved, etc.
	 * @return List<IssueEntity>
	 */

	@Transactional
	public List<IssueEntity> getIssuesByStatus(String status) {

		List<IssueEntity> issueList = new ArrayList<IssueEntity>();
		if (status.toUpperCase().equals("ALL")) {
			issueList = issueDao.getAllEagerIssueEntities();
		} else {

			StatusEntity statusEntity = statusDao.getStatusByName(status);
			if (statusEntity != null) {
				List<FieldsEntity> fieldsList = fieldsDao.getFieldsByStatusId(statusEntity.getIdStatus());

				for (FieldsEntity field : fieldsList) {
					IssueEntity issue = issueDao.getIssueByFieldsId(field.getIdFields());
					System.out.println(field.getIdFields());
					issue = issueDao.getEagerIssueByUniqueId(issue.getId());
					System.out.println("Inside getIssuesByStatus service " + issue);
					issueList.add(issue);
				}
			}
		}
		return issueList;
	}

	@Transactional
	public List<IssueEntity> getIssuesByStatusCode(StatusCode code) {

		List<IssueEntity> issueList = new ArrayList<IssueEntity>();

		List<FieldsEntity> fieldsList = fieldsDao.getFieldsByStatusId(code.getId());

		for (FieldsEntity field : fieldsList) {
			IssueEntity issue = issueDao.getIssueByFieldsId(field.getIdFields());
			issue = issueDao.getEagerIssueByUniqueId(issue.getId());
			issueList.add(issue);
		}

		return issueList;
	}

	@Transactional
	public List<IssueEntity> getIssuesByStatusAndDate(String status, Date dateFrom, Date dateTo) {

		List<IssueEntity> issueList = new ArrayList<IssueEntity>();

		StatusEntity statusEntity = statusDao.getStatusByName(status);
		if (statusEntity != null) {
			List<FieldsEntity> fieldsList = fieldsDao.getFieldsByStatusId(statusEntity.getIdStatus());

			for (FieldsEntity field : fieldsList) {
				IssueEntity issue = issueDao.getIssueByFieldsId(field.getIdFields());
				System.out.println(field.getIdFields());
				issue = issueDao.getEagerIssueByUniqueId(issue.getId());
				System.out.println("Inside getIssuesByStatus service " + issue);
				if (field.getCreated().after(dateFrom) && (field.getCreated().before(dateTo))) {
					issueList.add(issue);
				}
			}
		}
		return issueList;

	}
}
